﻿using System;
using System.Collections.Generic;
using System.Text;

namespace VOAEnglishDownload
{
    public class VOASpecialEnglish
    {
        public delegate void AnalyseFileCompletedEvent(String url, String fullPath);
        private AnalyseFileCompletedEvent analyseFileCompletedEvent = null;
        public AnalyseFileCompletedEvent AnalyseFileCompletedEvent1
        {
            get { return analyseFileCompletedEvent; }
            set { analyseFileCompletedEvent = value; }
        }


        private static List<String> allSubTypes = new List<string>();
        public static String [] AllSubTypes
        {
            get { return VOASpecialEnglish.allSubTypes.ToArray(); }
        }

        private static String domain = String.Empty;
        public static String Domain
        {
            get { return VOASpecialEnglish.domain; }
        }

        private static String voaType = String.Empty;
        public static String VoaType
        {
            get { return VOASpecialEnglish.voaType; }
        }

        static VOASpecialEnglish()
        {
            allSubTypes.Add("Development_Report_");
            allSubTypes.Add("This_is_America_");
            allSubTypes.Add("Agriculture_Report_");
            allSubTypes.Add("Science_in_the_News_");
            allSubTypes.Add("Health_Report_");
            allSubTypes.Add("Explorations_");
            allSubTypes.Add("Education_Report_");
            allSubTypes.Add("The_Making_of_a_Nation_");
            allSubTypes.Add("Economics_Report_");
            allSubTypes.Add("American_Mosaic_");
            allSubTypes.Add("In_the_News_");
            allSubTypes.Add("American_Stories_");
            allSubTypes.Add("Words_And_Their_Stories_");
            allSubTypes.Add("People_in_America_");
            allSubTypes.Add("Science_Report_");
            allSubTypes.Add("Intermediate_American_English_");

            domain = "http://www.21voa.com";
            voaType = "VOA_Special_English";
        }


        private int voaYear = 2007;
        public int VOAYear
        {
            get { return voaYear; }
            set { voaYear = value; }
        }

        private String saveDirectory = String.Empty;
        public String SaveDirectory
        {
            get { return saveDirectory; }
            set { saveDirectory = value; }
        }

        private Dictionary<String, String> allMp3s = new Dictionary<string, string>();
        public Dictionary<String, String> AllMp3s
        {
            get { return allMp3s; }
        }

        public void GetUrls()
        {
            for (int j = 0; j < allSubTypes.Count; ++j )
            {
                String subType = allSubTypes[j];
                String subUrl = domain + "/" + voaType + "_" + voaYear.ToString() + "/";
                String url = subUrl + "Index_" + subType + "1.html";
                List<String> mp3PageUrls = new List<string>();
                SharpDownloadSDK.HtmlParseUtilities.GetAllAdressesByUrlAndExts(url, ".html", ref mp3PageUrls);

                foreach (String mp3PageUrlTemp in mp3PageUrls)
                {
                    if (mp3PageUrlTemp.IndexOf(subType) == 0)
                    {
                        String mp3PageUrl = subUrl + mp3PageUrlTemp;
                        List<String> mp3Urls = new List<string>();
                        SharpDownloadSDK.HtmlParseUtilities.GetAllAdressesByUrlAndExts(mp3PageUrl, ".mp3", ref mp3Urls);
                        for (int i = 0; i < mp3Urls.Count; ++i)
                        {
                            String mp3Url = domain + mp3Urls[i];
                            int index = mp3Url.LastIndexOf('/');
                            if (index <= 0)
                                continue;
                            String mp3Name = mp3Url.Substring(index + 1);
                            String mp3FullPath = saveDirectory + voaYear.ToString() + "\\" + subType + "\\" + mp3Name;
                            allMp3s.Add(mp3Url, mp3FullPath);
                            if (AnalyseFileCompletedEvent1 != null)
                                AnalyseFileCompletedEvent1(mp3Url, mp3FullPath);
                        }
                    }
                }
            }
        }
    }
}
